package com.aeoj.clientmanagement.service.impl;

import com.aeoj.clientmanagement.domain.response.R;
import com.aeoj.clientmanagement.domain.user.SysUser;
import com.aeoj.clientmanagement.domain.user.dto.LoginUserDTO;
import com.aeoj.clientmanagement.domain.user.vo.SysUserVO;
import com.aeoj.clientmanagement.service.SysUserService;
import com.aeoj.clientmanagement.utils.security.BCryptPasswordEncoderUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aeoj.clientmanagement.mapper.SysUserMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;

/**
* @author 20620
* @description 针对表【sys_user(用户信息表)】的数据库操作Service实现
* @createDate 2025-04-16 21:53:03
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private BCryptPasswordEncoderUtil bCryptPasswordEncoderUtil;

    @Override
    public SysUser login(LoginUserDTO user) {
        SysUser loginUser = sysUserMapper.getSysUserByUsername(user);
        if (loginUser == null) return null;
        if (!bCryptPasswordEncoderUtil.matches(user.getPassword(), loginUser.getPassword()))
            return null;
        return loginUser;
    }

    @Override
    public SysUserVO getSysUserVOById(Long id) {
        return SysUserVO.toSysUserVO(this.getById(id)) ;
    }

    @Override
    public SysUser register(LoginUserDTO loginUserDTO) {
        SysUser sysUser = new SysUser();
        sysUser.setUserName(loginUserDTO.getUsername());
        sysUser.setNickName(loginUserDTO.getUsername());
        sysUser.setPassword(bCryptPasswordEncoderUtil.encode(loginUserDTO.getPassword()));
        int insert = sysUserMapper.insert(sysUser);
        return sysUser;
    }
}




